package com.zhang.realm;

import com.zhang.Token.JwtToken;
import com.zhang.checker.Resolver;
import com.zhang.dataSource.IDBServcice;
import com.zhang.principal.UserInfo;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.util.StringUtils;

public class DBRealm extends AuthorizingRealm {



    private Resolver resolver;

    public DBRealm(Resolver resolver) {
        this.resolver = resolver;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token){
        if (!(token instanceof JwtToken)) throw new RuntimeException("不支持的Token类型");

        JwtToken jwtToken = (JwtToken) token;
        if (jwtToken.getPrincipal() == null) throw new AuthenticationException("Token不能为空");

        //部分userInfo信息 jwtToken中不应该携带太多信息
        UserInfo partialUserInfo = resolver.resolve(jwtToken.getPrincipal());

        return new SimpleAuthenticationInfo(partialUserInfo,partialUserInfo,getName());
    }


    @Override
    protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
        return;
    }
}
